import QtQuick 1.1
import com.nokia.meego 1.0
import com.juhanapaavola.qmlcomponents 1.0


Page {        
    id: root
    tools: commonTools    
    property bool busy: false

    function runFirstTime(){
        busy = true
        bookPlugin.refreshBooks();
    }

    states:[
        State{
            name: "busy"
            when: busy
            PropertyChanges {
                target: commonTools
                visible: false
            }
            PropertyChanges {
                target: mainListView
                visible: false
            }
            PropertyChanges {
                target: busyIndicator
                visible: true
            }
        },
        State{
            name: "ready"
            when: !busy
            PropertyChanges {
                target: commonTools
                visible: true
            }
            PropertyChanges {
                target: mainListView
                visible: true
            }
            PropertyChanges {
                target: busyIndicator
                visible: false
            }
        }

    ]
    BookItemList{        
        id: bookPlugin

        property Book current;
        property string bname;
        property variant purl: undefined

        onBookOpen: {
            busy = false

            if(success){
                current = book
                bname = current.name
                purl = current.pages
                pageStack.push(readerPage, {imageList: purl,
                                   maxPageIndex: current.count-1,
                                   startPage: purl[0].pageUrl });                
            }else{
                errorDialog.text = qsTr("Error occured while opening book");
                errorDialog.open();
            }
        }
        onRefreshBooksDone: {
            busy = false
            mainListView.model = bookPlugin.books

            if(!foundBooks){
                errorDialog.text = qsTr("No books found from 'comicbooks' folder");
                errorDialog.open();
            }            
        }        
    }

    Component{
        id: bookItemDelegate

        Rectangle {
            id: bookItemDelegateRectangle

            height: itemImage.width
            width: parent.width
            color: "black"
            radius: 10

            Row {
                id: row1
                spacing: 10
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.leftMargin: 4
                anchors.rightMargin: 4

                Image {
                    id: itemImage
                    source: image
                    width: 128
                    height: 128
                    anchors.verticalCenter: parent.verticalCenter
                }

                Text {
                    width: parent.width-itemImage.width
                    text: name
                    wrapMode: "WordWrap"
                    anchors.verticalCenter: parent.verticalCenter
                    font.bold: true
                    font.pixelSize: itemImage.height/4
                    color: "white"
                }
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    animColor.running = true
                }
            }

            PropertyAnimation{
                id: animColor
                target: bookItemDelegateRectangle
                properties: "color"
                to: "steelblue"
                duration: 200

                onRunningChanged: {
                    if(!running){
                        mainListView.currentIndex = index
                        animColorBack.running = true
                    }
                }
            }

            PropertyAnimation{
                id: animColorBack
                target: bookItemDelegateRectangle
                properties: "color"
                to: "black"
                duration: 200
            }
        }
    }

    ListView {
        id: mainListView
        anchors.fill: parent
        delegate: bookItemDelegate
        model: bookPlugin.books
        currentIndex: -1

        onCurrentIndexChanged: {            
            if(currentIndex>-1){
                busy = true
                bookPlugin.openBook(currentIndex);                
                mainListView.currentIndex = -1
            }
        }        
    }

    ToolBarLayout {
        id: commonTools
        visible: true
        anchors.bottom: parent.bottom
        ToolIcon{
            platformIconId: "toolbar-back";
            onClicked: {
                Qt.quit();
            }
        }

        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status === DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    }

    Menu {
        id: myMenu
        visualParent: mainPage
        MenuLayout {
            MenuItem { text: qsTr("Refresh library");
            onClicked: {
                busy = true
                bookPlugin.refreshBooks();
                }
            }
            MenuItem { text: qsTr("About");
                onClicked: {
                    aboutDialog.open();
                }
            }
        }
    }

    BusyIndicator{
        id: busyIndicator
        anchors.centerIn: parent
        platformStyle: BusyIndicatorStyle { size: "large" }
        running: true
        visible: false
    }


    TextDialog{
        id: aboutDialog
    }

    ErrorDialog{
        id: errorDialog
    }

}
